package com.ruoyi.web.controller.business.rule;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.business.rule.domain.JbOrderCoefficientRules;
import com.ruoyi.business.rule.domain.vo.JbOrderCoefficientRulesVo;
import com.ruoyi.business.rule.service.JbOrderCoefficientRulesService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.web.controller.business.rule.vo.JbOpportunityRulesVO;
import com.ruoyi.web.controller.business.rule.vo.JbOrderCodefficientRulesVO;
import com.ruoyi.web.core.JoController;
import com.ruoyi.web.core.JoR;
import com.ruoyi.web.core.tool.ColumnUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * 订单系数规则 Controller
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Api("业务规则")
@RestController
@RequestMapping("/orderCoefficientRules")
public class JbOrderCoefficientRulesController extends JoController
{
    @Autowired
    private JbOrderCoefficientRulesService service;
    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询业务规则列表
     */
    @ApiOperation("查询业务规则列表")
    @GetMapping("/list")
    public TableDataInfo list(PageParam page, JbOrderCoefficientRules jbRules) throws InvocationTargetException,
            NoSuchMethodException, IllegalAccessException
    {
        LambdaQueryWrapper<JbOrderCoefficientRules> select = new LambdaQueryWrapper<>();
        IPage<JbOrderCoefficientRules> iPage = service.page(page.b(), select);
        List<JbOrderCodefficientRulesVO> list = BeanUtil.copyToList(iPage.getRecords(),
                JbOrderCodefficientRulesVO.class);

        //获取数据集
        List<Long> ids = ids(iPage.getRecords(), ColumnUtil.F(JbOpportunityRulesVO::getDeptId));
        List<SysDept> depts = deptService.listIds(ids);

        //规则
        Map map = kv(
                ColumnUtil.F(JbOrderCodefficientRulesVO::getDeptId),
                ColumnUtil.F(JbOrderCodefficientRulesVO::getDeptName),
                depts
        );
        toKey(list, map,
                ColumnUtil.F(JbOrderCodefficientRulesVO::getDeptId),
                ColumnUtil.F(JbOrderCodefficientRulesVO::getDeptName)
        );
        TableDataInfo tableDataInfo = JoR.rows(iPage);
        tableDataInfo.setRows(list);
        return tableDataInfo;
    }

    /**
     * 查询业务规则列表
     */
    @ApiOperation("查询业务规则列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(JbOrderCoefficientRules jbRules)
    {
        List<JbOrderCoefficientRulesVo> list = service.listAll(jbRules);
        return success(list);
    }

    /**
     * 新增业务规则
     */
    @ApiOperation("新增业务规则")
    @PreAuthorize("@ss.hasPermi('rules:change')")
    @Log(title = "业务规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JbOrderCoefficientRules jbRules)
    {
        return toAjax(service.save(jbRules));
    }

    /**
     * 修改业务规则
     */
    @ApiOperation("修改业务规则")
    @PreAuthorize("@ss.hasPermi('rules:change')")
    @Log(title = "业务规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JbOrderCoefficientRules jbRules)
    {
        if (ObjectUtils.isEmpty(jbRules.getDeptId()))
        {
            return AjaxResult.error("部门不能为空");
        }
        JbOrderCoefficientRules one = service.getOne(Wrappers.lambdaQuery(JbOrderCoefficientRules.class)
                .eq(JbOrderCoefficientRules::getDeptId, jbRules.getDeptId()), false);
        if (ObjectUtils.isNotEmpty(one))
        {
            jbRules.setId(one.getId());
        }
        return toAjax(service.saveOrUpdate(jbRules));
    }

    /**
     * 获取业务规则详细信息
     */
    @ApiOperation("获取业务规则详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(service.getById(id));
    }

    /**
     * 获取业务规则详细信息
     */
    @ApiOperation("获取业务规则详细信息")
    @GetMapping(value = "/dept/{deptId}")
    public AjaxResult getDeptInfo(@PathVariable("deptId") Long deptId)
    {
        return success(service.getOne(Wrappers.lambdaQuery(JbOrderCoefficientRules.class)
                .eq(JbOrderCoefficientRules::getDeptId, deptId), false));
    }
}
